package com.javasm.controller;

import com.javasm.entity.User;
import com.javasm.service.UserService;
import com.javasm.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/user")
public class QueryUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        User loginUser = (User) session.getAttribute("loginUser");

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(loginUser != null){
            UserService uS = new UserServiceImpl();
            List<User> listuser = uS.listuser();

            //然后给页面做反馈


            //通过输出流拼页面
            writer.print("<table border='15px'>");
            writer.print("<tr>");
            writer.print("<th>编号</th>");
            writer.print("<th>姓名</th>");
            writer.print("<th>年龄</th>");
            writer.print("<th>密码</th>");
            writer.print("</tr>");

            for(User user:listuser){
                writer.print("<tr>");
                writer.print("<td>"+user.getId()+"</td>");
                writer.print("<td>"+user.getUsername()+"</td>");
                writer.print("<td>"+user.getAge()+"</td>");
                writer.print("<td>"+user.getPassword()+"</td>");
                writer.print("</tr>");
            }

            writer.print("</table>");
            writer.close();
        }else{
            writer.println("请先登录，精彩内容等着你哦！！！！！！！！！！");

            writer.println("------<script>setTimeout(()=>{ location.href='/day10/loginpage.html'},2000);</script>");
            writer.close();
        }


        }




}
